package com.hatic.process;

import java.util.List;
import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.MapUtil;

/**
 * 读取项目的进度计划数据与相关数据：
 * 取项目进度实际数据；
 * 保存实际开始与结束时间，保存实际情况数据；
 *
 * @author TonyTan
 * @version 1.0, 2011-10-24
 */
public class ProjectPlanJson extends BusinessObject {
	private static final long serialVersionUID = 1L;
	
	private static String PLANSQL = "select plan_id, task_no, task_content, " +
		"start_date, end_date, project_day, project_user, project_userid, " +
		"is_milestone, parent_no, pre_no, is_pay, " +
		"done_sdate, done_edate, done_day, done_memo, done_ratio " +
		"from project_plan where mplan_id = ? " +
		"order by task_no";

	/**
	 * 查询任务计划数据
	 * @param mplanId
	 * @return
	 */
	public String queryTask(String mplanId) {
		List<Map<String, String>> plandata = queryData(mplanId);
		
		StringBuilder sbDepend = new StringBuilder();
		for (int i = 0, n = plandata.size(); i < n; i++) {
			Map<String, String> mpdata = plandata.get(i);
			
			String Id = mpdata.get("task_no"); 
			String Name = mpdata.get("task_content");
			String StartDate = mpdata.get("done_sdate");
			String EndDate = mpdata.get("done_edate");
			String PercentDone = mpdata.get("done_ratio");
			String ParentId = mpdata.get("parent_no");
			String IsLeaf = Boolean.toString((ParentId.length() > 0));
			String Responsible = MapUtil.getValue(mpdata, "project_user");
			String Duration = mpdata.get("done_day");
			String PlanId = mpdata.get("plan_id"); 
			
			//父节点ID如果为空，必须用null，否则图形不能打开
			if (ParentId.length() == 0) ParentId = "null";
			if (PercentDone == null || PercentDone.length() == 0) PercentDone = "0";
			
			sbDepend.append("{Id:" + Id + ",");
			sbDepend.append("Name:'" + Name + "',");
			sbDepend.append("PlanId:'" + PlanId + "',");
			sbDepend.append("StartDate:'" + StartDate + "',");
			sbDepend.append("EndDate:'" + EndDate + "',");
			sbDepend.append("PercentDone:" + PercentDone + ",");
			sbDepend.append("ParentId:" + ParentId + ",");
			sbDepend.append("IsLeaf:" + IsLeaf + ",");
			sbDepend.append("Responsible:'" + Responsible + "',");
			sbDepend.append("Duration:"+ Duration +"}\r\n,");
		}
		String strJson = "";
		if (sbDepend.length() > 0) {
			strJson = sbDepend.substring(0, sbDepend.length()-1);
		}
		
		StringBuilder sbJson = new StringBuilder("[").append(strJson + "]");
		_log.showDebug("json=" + sbJson.toString());
		
		//返回查询数据
		setReturnData(sbJson.toString());
		
		return _returnSuccess;
	}
	
	/**
	 * 任务之间的依赖数据
	 * @param mplanId
	 * @return
	 */
	public String queryDepend(String mplanId) {
		List<Map<String, String>> plandata = queryData(mplanId);
		
		StringBuilder sbDepend = new StringBuilder();
		for (int i = 0, n = plandata.size(); i < n; i++) {
			Map<String, String> mpdata = plandata.get(i);
			
			String Id = mpdata.get("task_no"); 
			String From = mpdata.get("pre_no");
			if (From.length() == 0) continue;
			
			sbDepend.append("{From:" + From + ",");
			sbDepend.append("To:" + Id + ",");
			sbDepend.append("Type:2}\r\n,");
		}
		String strJson = "";
		if (sbDepend.length() > 1) {
			strJson = sbDepend.substring(0, sbDepend.length()-1);
		}
		
		StringBuilder sbJson = new StringBuilder("[").append(strJson + "]");
		_log.showDebug("json=" + sbJson.toString());
		
		//返回查询数据
		setReturnData(sbJson.toString());
		
		return _returnSuccess;
	}
	
	/**
	 * 查询项目进度计划数据
	 * @param mplanId
	 * @return
	 */
	private List<Map<String, String>> queryData(String mplanId) {
		DaoParam param = _dao.createParam(PLANSQL);
		param.addStringValue(mplanId);
		
		return _dao.query(param);
	}
}
